import { _decorator, Component,PhysicsSystem2D, Contact2DType,Node, Input, input, EventTouch, Animation, Label } from 'cc';
import { AudioManager } from './AudioManager';
import { BlocksMap } from './BlocksMap';
import { PlayerController } from './PlayerController';
const { ccclass, property } = _decorator;

@ccclass('GameManage')
export class GameManage extends Component {

    @property({ type: PlayerController })
    player: PlayerController = null;

    @property({ type: BlocksMap })
    blockMap: BlocksMap = null;

    @property(Node)
    startMenu: Node = null

    @property(Node)
    overMenu: Node = null

    @property(Node)
    scorelabelNode: Node = null

    @property(Label)
    scorelabel: Label = null

    @property(Label)
    stopScorelabel: Label = null

    @property({ type: AudioManager })
    backMusic: AudioManager = null


    start() {
        // this.blockMap.genBlogs()
        // this.player.startGame()
        // PhysicsSystem2D.instance.on(Contact2DType.BEGIN_CONTACT, this.stopGame, this)
        this.scorelabelNode.active = false
        this.overMenu.active = false
    }

    update(deltaTime: number) {

    }

    stopGame() {
        this.setScoreLabel(false)
        this.blockMap.stop()
        this.player.init(false)
        this.overMenu.active = true
        this.backMusic.stopMusic()
        // this.scorelabelNode.active = false
    }

    startGame(){
        this.backMusic.playMusic()
        this.startMenu.active = false
        this.overMenu.active = false
        this.scorelabelNode.active = true
        this.player.init(true)
        this.blockMap.init()
        this.setScoreLabel(true)
    }


    score: number = 0;
    interval: number = null;

    setScoreLabel(run: boolean){
        if(run){
            this.score = 0
            this.scorelabel.string = '0'
            setTimeout(()=>{
                this.interval = setInterval(()=>{
                    this.score = Math.floor(this.score + this.blockMap.speed)
                    this.scorelabel.string = this.score + ''
                }, 100)
            },1000)
        }else{
            this.stopScorelabel.string = this.score + ''
            clearInterval(this.interval)
        }
    }

}


